package com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.useraction;

import android.graphics.drawable.GradientDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import com.example.dante.a17376060_wanghaikang_finaltest.R;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.adapter.NewsAdapter;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.Name;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.News;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.domain.User;
import com.example.dante.a17376060_wanghaikang_finaltest.cn.example.main.cn.example.utils.HttpUtil;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Set;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class ShouCang extends AppCompatActivity {

    RecyclerView recyclerView;
    List<News> newsList=new ArrayList<>();
    String responseData;
    String username;
    List<User> userList=new ArrayList<>();
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    recyclerView.addItemDecoration(new DividerItemDecoration(getApplicationContext(), DividerItemDecoration.VERTICAL));
                    NewsAdapter newsAdapter = new NewsAdapter(newsList, getApplicationContext());
                    recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                    recyclerView.setAdapter(newsAdapter);
                    break;
            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shou_cang);
        Toolbar toolbar = findViewById(R.id.shoucang_toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("收藏夹");
        initView();
    }

    private void initView() {
        recyclerView=findViewById(R.id.shoucang_news);
        okhttp();
    }

    private void okhttp() {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    username=Name.USER_NAME;
                    HttpUtil.sendOkHttpRequest("http://47.101.175.33:8080/AndroidDataFinalTest/news_findByUser.action?user_name="+username, new Callback() {
                        @Override
                        public void onFailure(Call call, IOException e) {

                        }

                        @Override
                        public void onResponse(Call call, Response response) throws IOException {
                            responseData = response.body().string();
                            parseJSONObeject(responseData);
                        }
                    });
                }
            }).start();
        }


    private void parseJSONObeject(String responseData) {
        Gson gson=new Gson();
        userList = gson.fromJson(responseData, new TypeToken<List<User>>() {
        }.getType());
        for(User user:userList){
            Set<News> newsSet=user.getNews();
            newsList=new ArrayList<>(newsSet);
        }
//        try{
//            JSONArray jsonArray=new JSONArray(responseData);
//            Log.d("ergouzi",""+jsonArray.length());
//            for(int i=0;i<jsonArray.length();i++){
//                JSONObject jsonObject=jsonArray.getJSONObject(i);
//                id=jsonObject.getInt("news_id");
//                name=jsonObject.getString("news_title");
//                content=jsonObject.getString("news_content");
//                image=jsonObject.getString("new_image");
//                kinds=jsonObject.getString("news_kinds");
//                news.setNews_kinds(kinds);
//                news.setNews_id(id);
//                news.setNews_title(name);
//                news.setNews_content(content);
//                news.setNew_image(image);
//                Log.d("测试",news.toString());
//            }
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//        newsList.add(news);
        Message msg = new Message();
        msg.what = 1;
        handler.sendMessage(msg);
    }
}
